#include "MyObjects.h"
#include "Util/Texture.h"
#include "Util/GameLib.h"
#include "Util/ObjectController.h"
#include "Util/Logger.h"
#include "Util/Shapes3D/Plane3D.h"
#include "Util/Light.h"
#include "Util/Shapes3D/Vector3D.h"
#include "Util/DisplayList.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Util/Sprite.h"
#include "OpenGL.h"

#include <stdio.h>
#include <assert.h>

Object *myLight;
Object *myCamera;
Sprite heart;
Sprite aButton;
DisplayList terrList;

void createObjects()
{
    lightStudio.setGlobalAmbientLight(C_BLACK);
	myLight = objectController.create(new MyLight());
    myCamera = objectController.createCamera(new MyCam());
    objectController.create(new MyObj());
	heart.add("/Users/jake/Desktop/heart.png");
	//aButton.add("/Users/jake/Desktop/heart.png");
	//aButton.add("/Users/jake/Desktop/Gamecube_Button_A.tiff");
	Point3D pt;
	pt.set(1,2,3);
	mainLog.print(Str()<<pt.toString());
}


void MyObj::create()
{
	srand((unsigned)time(NULL));
	rot=0.0;
	//direction.fromLength(0,0,1);
	position.set(0,0,50);
	//test.load("/Users/jake/Downloads/model_NO_ANIMATION.ma");
	test.load("/Users/jake/Desktop/boblampclean.md5");
	
	size = 50;
	/*FILE *f;
	f = fopen("/Users/jake/Dropbox/EFB/terr.tsv", "r");
	assert(f>0);
	Str line,part;
	char buffer[1000];
	for (int y=0;y<size;y++)
	{
		fgets(buffer, 1000, f);
		line = buffer;
		fgets(buffer, 1000, f);
		//mainLog.print(line);
		for (int x=0;x<size;x++)
		{
			part.getFromMark("\t", line);
			double val;
			val = part.atod()/10;
			mainLog.print(Str()<<"x: "<<x<<" y: "<<y<<" val: "<<val);
			terr[x][y].set(x*size,y*size,val);
		}
	}
	//double prec = 1;
	terrList.start();
	for (int y=0;y<size-1;y++)
	{
		for (int x=0;x<size-1;x++)
		{
			Plane3D()
			.set(terr[x][y],terr[x][y+1],terr[x+1][y+1],terr[x+1][y])
			.drawSolid();//drawTessellated(prec, prec);
		}
	}
	terrList.end();*/
}

void MyObj::draw()
{
	rot+=1.0;
	
	//GameLib::setColor(C_BLUE);
	test.draw(position,0,0,0);//objectController.getMousePosition().getX(),objectController.getMousePosition().getY(),0);
	
	gameLib.setColor(C_GREEN2);
	gameLib.setShine(60.0);
	
	//terrList.draw();
}

void MyObj::drawOrtho()
{
	//Plane3D().set(Point3D().set(0, 0, 0), Point3D().set(-1, 0, 0), Point3D().set(-1, 1, 0), Point3D().set(0, 1, 0)).drawSolid_color(C_RED);
	double pos = -1;
	for (int hearts=0;hearts<8;hearts++)
	{
		pos += heart.drawH(Point2D().set(pos, 0.9), 0, 0.1)+0.01;
		//mainLog.print(Str()<<"pos = "<<pos);
	}
	aButton.drawW(Point2D().set(0.8,0.8), 0, 0.2);
}



void MyLight::create()
{
    position.set(0,0,150);
    ambient.setColor(C_WHITE);
    //ambient.setIntensity(1.0);
    ambient.enable();
    diffuse.enable();
    //specular.enable();
}

void MyLight::step()
{
	double dir;
	double x=0.0,y=0.0,z=0.0;
	double speed = 20.0;
	if (objectController.getKeyState('w'))
	{y+=1.0;}
	if (objectController.getKeyState('a'))
	{x-=1.0;}
	if (objectController.getKeyState('s'))
	{y-=1.0;}
	if (objectController.getKeyState('d'))
	{x+=1.0;}
	if (objectController.getKeyState('r'))
	{z+=1.0;}
	if (objectController.getKeyState('f'))
	{z-=1.0;}
	if (x==0.0 && y==0.0 && z==0.0){return;}
	dir = RAD2DEG(direction.yxDir()-myCamera->direction.yxDir());
	
	position.move(Direction3D().setFromLength(x,y,z).rotateD(dir,0), speed);
}

/*void MyLight::draw()
{
	//position.draw_sizeColor(5, C_YELLOW);
}*/

void MyCam::create()
{
	setFar(5000);
    position.set(-100, -100, 200);
	lockOnObject(myLight);
	lockAngleVertical();
}

void MyCam::step()
{
	//Str t;
	//mainLog.print(Str()<<"The cameras new position is: "<<position.toString(t));
	if (objectController.getKeyState(KEY_LEFT))
	{rotateAroundD(-5, 0);}
	if (objectController.getKeyState(KEY_RIGHT))
	{rotateAroundD(5, 0);}
	if (objectController.getKeyState(KEY_UP))
	{rotateAroundD(0, 5);}
	if (objectController.getKeyState(KEY_DOWN))
	{rotateAroundD(0, -5);}
	if (objectController.getKeyState(KEY_PAGE_UP))
	{zoomExp(0.9);}
	if (objectController.getKeyState(KEY_PAGE_DOWN))
	{zoomExp(1.1);}
}
